package org.nokatag.tagjava;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.password.AESPlus;
import org.nokatag.system.BugInfoWrint;
import org.nokatag.system.Concest;
import org.nokatag.system.DBCon;
import org.nokatag.system.Function;
import org.nokatag.system.RunUserFunction;

/**
 * check选择框组
 * @author xiefangjian
 *
 */
public class Checks extends Inputsuper{

	private static final long serialVersionUID = -7693662152543679408L;
	
	private String id = null;
	private String name = null;
	private String sql = null;//sql方式加载
	private String json = null;//json方式加载 [{id:sss,f},{value:dd}]
	private String onclick=null;//值改变调用外部js
	private String value = null;
	private String readonly=null;//只读
	private String selectsun ="0";
	private String formatcell=null;
	private String classstyle = null;
	private String style  = null;
	private String stype = "right";
	private String width = "120";
	private String disabled = null;
	private String divstyle= null;
	private String divclassstyle=null;
	private String stopcheck = null;//单个选项禁用
	
	
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		StringBuilder body = new StringBuilder();
		String basePath = basepath(request);
		//--------------------------------------------------------
		String vs=options(request);
		if(vs!=null && vs.trim().length()>0){
			FormHtmlInsertid(getId(),getName(),getValue());
			body.append(potjs(request));//js框架
			body.append(nkrchecksJS(request));//nrkchecks js
			body.append(selectCss(request));
			body.append("<div id=\""+id+"\" "+(divstyle==null?"":"style=\""+divstyle+"\"")+" "+(divclassstyle==null?"class=\"nk_input_sdDiv\"":"class=\""+divclassstyle+"\"")+"></div>");
			body.append("<script type=\"text/javascript\">");
			body.append("Event.observe(document, 'dom:loaded', function() {");
			body.append("var nkrch"+id+" = {");
			body.append("id:'"+id+"',");//id
			body.append("rurl:'"+basePath+"',");//根url
			body.append("width:"+LenNumber(width)+",");//宽
			body.append("name:'"+(name==null?id:name)+"',");//名字
			body.append("json:"+(vs==null?"[]":vs.replace("'", "\""))+",");//初始始值
			body.append("value:"+value()+",");//值
			body.append("readonly:'"+(readonly==null?"":readonly)+"',");//只读
			body.append("selectsun:"+selectsun+",");
			if(null!=divstyle){
				body.append("divstyle:'"+divstyle+"',");
			}
			if(null!=divclassstyle){
				body.append("divclassstyle:'"+divclassstyle+"',");
			}
			if(null!=stopcheck){
				body.append("stopcheck:'"+stopcheck+"',");//单个选项禁用
			}
			body.append("ctype:'checkbox',");
			body.append("stype:'"+stype+"',");
			body.append("disabled:'"+(disabled==null?"":disabled)+"',");
			body.append("classstyle:'"+(classstyle==null?"":classstyle)+"',");
			body.append("style:'"+(style==null?"":style)+"',");
			body.append("onclick:function(self){"+JSFunction(onclick)+"},");//onchange
			body.append("sql:'"+(sql==null?"":AESPlus.encrypt(sql, Concest.SQLKEY))+"'");//sql
			body.append("};");
			body.append("var nkr"+id+" = new nkrchecks(nkrch"+id+");");
			body.append("nkr"+id+".show();");
			body.append("});");
			body.append("</script>");
		}
		//--------------------------------------------------------
		try {
			jspOut.write(body.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	private String value(){
		if(value!=null && value.trim().length()>0){
			if(!value.startsWith("[")){
				value = "['"+value.replace(",", "','")+"']";
				return value;
			}
		}
		return "[]";
	}
	//----------------------初始化值---------------------------------------------------
	private String options(HttpServletRequest request){
		if(json!=null && json.trim().length()>0){
			return json;//控件组
		}
		if(getSql()!=null && sql.trim().length()>10){
			try{
				Connection con = DBCon.getCon();
				con.setAutoCommit(true);
				Statement sta = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
				ResultSet rst = sta.executeQuery(sql);
				ResultSetMetaData rd = rst.getMetaData();
				StringBuffer body = new StringBuffer();
				body.append("[");
				while(rst.next()){
					String value = (rst.getString("value")==null?"":rst.getString("value"));
					String label = (rst.getString("label")==null?"":rst.getString("label"));
					String name = null;
					String nid = null;
					if(Function.TableFieldIsExit(rd, "name")) name = rst.getString("name");
					if(Function.TableFieldIsExit(rd, "nid")) nid = rst.getString("nid");
					name =(name==null?getName():name);
					if(formatcell!=null && formatcell.trim().length()>0){
						for(String fun:formatcell.split("\\|")){
							String cn = fun.substring(fun.indexOf(" as ")+" as ".length(),fun.length());
							fun=fun.split(" as ")[0];
							String v=RunUserFunction.TagFunction(fun, request, rst);
							if(cn.equals("value")){
								value= v;
							}else if(cn.equals("label")){
								label = v;
							}else if(cn.equals("name")){
								name = v;
							}else if(cn.equals("onclick")){
								onclick = v;
							}
						}
					}
					body.append("{");//[{value=fds,label=fds,name=abc},{value=fds,label=fds,name=abc}]
					body.append("value:'"+value+"',");
					body.append("label:'"+label+"',");
					if(nid!=null && nid.trim().length()>0)
						body.append("id:'"+nid+"',");
					body.append("name:'"+name+"',");
					body.append("},");
				}
				if(body.length()>3)
					body.delete(body.length()-1, body.length());
				body.append("]");
				rst.close();
				sta.close();
				return body.toString();
			}catch(Exception se){
				BugInfoWrint.Print(se);
			}
		}
		return "[]";
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public String getOnclick() {
		return onclick;
	}
	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getReadonly() {
		return readonly;
	}
	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}
	public String getFormatcell() {
		return formatcell;
	}
	public void setFormatcell(String formatcell) {
		this.formatcell = formatcell;
	}
	public String getClassstyle() {
		return classstyle;
	}
	public void setClassstyle(String classstyle) {
		this.classstyle = classstyle;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public String getStype() {
		return stype;
	}
	public void setStype(String stype) {
		this.stype = stype;
	}
	public String getDisabled() {
		return disabled;
	}
	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public String getSelectsun() {
		return selectsun;
	}
	public void setSelectsun(String selectsun) {
		this.selectsun = selectsun;
	}
	public String getDivstyle() {
		return divstyle;
	}
	public void setDivstyle(String divstyle) {
		this.divstyle = divstyle;
	}
	public String getDivclassstyle() {
		return divclassstyle;
	}
	public void setDivclassstyle(String divclassstyle) {
		this.divclassstyle = divclassstyle;
	}
	public String getStopcheck() {
		return stopcheck;
	}
	public void setStopcheck(String stopcheck) {
		this.stopcheck = stopcheck;
	}
	
}